﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Drawing;

namespace Engine
{
	class CPowerUp
	{
		private   int			m_iType;
		private   int			m_iValue;
		private   int			m_iX;
		private   int			m_iY;
		protected CWorld		m_oWorld;
		private   CAnimationObj	m_oAnimObj;

		protected static int	s_iCount = 0;

		private System.Timers.Timer m_oTimer;

		public CPowerUp(int iX, int iY, int iType, Bitmap oAnim, int iSeq, bool bPerma)
		{
			m_oWorld = null;
			Random	oRand	 = new Random();

			//Save pos for deletion
			m_iX = iX;
			m_iY = iY;

			//Value
			m_iValue = 0;

			//Type
			m_iType = iType;

			//Suicide timer (onyl if not perma)
			if(!bPerma)
			{
				m_oTimer          = new System.Timers.Timer(oRand.Next(12000, 21000));
				m_oTimer.Elapsed += new ElapsedEventHandler(DeleteTimeEvent);
				m_oTimer.Start();
			}

			//Animation
			m_oAnimObj = new CAnimationObj(null, iX, iY);
			CAnimationCore.SetAnimObj(m_oAnimObj);
			m_oAnimObj.SetTickOffset(3);
			m_oAnimObj.SetAnimation(oAnim, iSeq, Const.ANIM_STATION, true);

			s_iCount++;
		}

//////////////////////////////////////////////////////////////////////////////////////////////////////
////////// UTILITIES

		//Destroy animation object, effectively ending all animation
		// - Can only be called once
		//@ KR / 2014.07.09
		public void Destroy()
		{
			if(m_oAnimObj != null)
				CAnimationCore.RemoveAnimObj(m_oAnimObj.GetID());
		}

		//Set world for deletion call
		//@ KR / 2014.07.09
		public void SetWorld(CWorld oWorld)
		{
			m_oWorld = oWorld;
		}

		//Get PowerUp Type
		// - Need to overwerite default
		//@ KR / 2014.07.09
		public int GetPowerUpType()
		{
			return m_iType;
		}

		//Get static count of random elements
		//@ KR / 2014.07.09
		public static int GetCount()
		{
			return s_iCount;
		}

//////////////////////////////////////////////////////////////////////////////////////////////////////
////////// VALUE

		//Set power up value
		//@ KR / 2014.07.09
		protected void SetValue(int iValue)
		{		
			m_iValue = iValue;
		}

		//Retrieve power up value
		//@ KR / 2014.07.09
		public int GetValue()
		{		
			return m_iValue;
		}

//////////////////////////////////////////////////////////////////////////////////////////////////////
////////// TIMERS

		//Timer for power up deletion
		//@ KR / 2014.07.09
		private void DeleteTimeEvent(object source, ElapsedEventArgs e)
		{
			m_oTimer.Stop();
			s_iCount--;
			m_oWorld.RemovePowerUp(m_iX, m_iY);
		}
	}
}
